import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {baseDataStatus, claimReasonType} from '../Enum';
import {FORM_OPTIONS} from '../constants';
import TextInput from 'Shared/components/TextInput';
import {PERMISSION} from './constants';
import routes from './routes';
import WrappedSelect from '../common/WrappedSelect';
import {injectIntl, language} from './intl';

const FormItem = Form.Item;
const baseDataStatusToList = baseDataStatus.toList(language);
const claimReasonTypeToList = claimReasonType.toList(language);

class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }

    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }

    render() {
        const {formatMessage} = this.props.intl;
        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'queryPanel.form.type',
                            defaultMessage: '原因类型'
                        })} {...FORM_OPTIONS.item}>
                            <WrappedSelect
                                id="type"
                                allowClear={true}
                                value={this.props.conditions.type}
                                onChange={this.handleFilterChange}
                                options={claimReasonTypeToList} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'queryPanel.form.code',
                            defaultMessage: '编号'
                        })}{...FORM_OPTIONS.item}>
                            <TextInput
                                name="code"
                                value={this.props.conditions.code}
                                onPressEnter={this.handleFilterPressEnter}
                                onChange={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'queryPanel.form.description',
                            defaultMessage: '原因描述'
                        })}{...FORM_OPTIONS.item}>
                            <TextInput
                                name="description"
                                value={this.props.conditions.description}
                                onPressEnter={this.handleFilterPressEnter}
                                onChange={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'queryPanel.form.status',
                            defaultMessage: '状态'
                        })} {...FORM_OPTIONS.item}>
                            <WrappedSelect
                                id="status"
                                allowClear={true}
                                value={this.props.conditions.status}
                                onChange={this.handleFilterChange}
                                options={baseDataStatusToList} />
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                            id: 'queryPanel.btn.query',
                            defaultMessage: '查询'
                        })}</Button>
                        <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                            id: 'queryPanel.btn.reset',
                            defaultMessage: '重置'
                        })}</Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        {this.props.addable && <Button key="add" type="primary" href={`#${routes.add.url()}`}>{formatMessage({
                            id: 'queryPanel.btn.add',
                            defaultMessage: '新增'
                        })}</Button>}
                    </Col>
                </Row>
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    addable: PropTypes.bool,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);

const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    addable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.add),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
